package com.xcx.eduservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xcx.commonuntils.returnuntls.ErrorEnum;
import com.xcx.commonuntils.returnuntls.ReturnMessage;
import com.xcx.commonuntils.returnuntls.ReturnMessageUtils;
import com.xcx.eduservice.entity.EduTeacher;
import com.xcx.eduservice.request.TeacherQuery;
import com.xcx.eduservice.service.EduTeacherService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-10-08
 */
@Api(tags = "讲师管理系统")
@RestController
@RequestMapping("/edu-teacher")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class EduTeacherController {
    final private EduTeacherService eduTeacherService;

    //查询所有讲师列表
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public ReturnMessage<Object> findAll() {

        List<EduTeacher> list = eduTeacherService.list(null);

        return ReturnMessageUtils.success(list);

    }




    @ApiOperation(value = "有条件分页查询讲师")

    @PostMapping("/pageTeacherCondition")
    public ReturnMessage<IPage<EduTeacher>> pageTeacherCondition(@RequestBody @Valid TeacherQuery request, BindingResult br) {


        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return eduTeacherService.pageTeacherCondition(request);


    }


}

